package net.neiquanlib.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import net.neiquanlib.R;

/**
 * 作者 : hjb
 * 时间 : 2016/6/24.
 */
public class HorizontalEditTextItem extends LinearLayout {
    private Context mContext;
    private float mDensity;
    private static final int DEFAULT_CORNER_DISTANCE = 15;
    private static final int DEFAULT_EDIT_SIZE = 15;
    private static final int DEFAULT_LEFT_TEXT_SIZE = 16;
    private static final int DEFAULT_IMAGE_SIZE = 28;
    private static final int DEFAULT_TAG_BACKGROUND_COLOR = 0xff545454;
    private int edit_text_color;
    private int left_text_color;
    private int left_img_visible;
    private int left_text_visible;
    private int edit_text_size;
    private int left_text_size;
    private int left_img_size;
    private String edit_hint_text;
    private String edit_text;
    private String left_text;
    private int left_img;
    private View mView;
    private int background;
    private LinearLayout root_ll;
    private ImageView mLeftImg;
    private TextView mLeftText;
    private EditText mEditText;

    public HorizontalEditTextItem(Context context) {
        this(context, null);
    }

    public HorizontalEditTextItem(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public HorizontalEditTextItem(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        mDensity = context.getResources().getDisplayMetrics().density;
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.HorizontalEditTextItem, defStyleAttr, 0);
        edit_hint_text = a.getString(R.styleable.HorizontalEditTextItem_edit_hint_text);
        edit_text = a.getString(R.styleable.HorizontalEditTextItem_edit_text);
        left_text = a.getString(R.styleable.HorizontalEditTextItem_left_text);
        edit_text_color = a.getColor(R.styleable.HorizontalEditTextItem_edit_text_color, DEFAULT_TAG_BACKGROUND_COLOR);
        left_text_color = a.getColor(R.styleable.HorizontalEditTextItem_left_text_color, DEFAULT_TAG_BACKGROUND_COLOR);
        left_img_visible = a.getInteger(R.styleable.HorizontalEditTextItem_edit_left_img_visible, 0);
        left_text_visible = a.getInteger(R.styleable.HorizontalEditTextItem_left_text_visible, 0);
        edit_text_size = a.getDimensionPixelSize(R.styleable.HorizontalEditTextItem_edit_text_size, dip2px(DEFAULT_EDIT_SIZE));
        left_text_size = a.getDimensionPixelSize(R.styleable.HorizontalEditTextItem_left_text_size, dip2px(DEFAULT_LEFT_TEXT_SIZE));
        left_img_size = a.getDimensionPixelSize(R.styleable.HorizontalEditTextItem_edit_left_img_size, dip2px(DEFAULT_IMAGE_SIZE));
        left_img = a.getResourceId(R.styleable.HorizontalEditTextItem_edit_left_img, 0);
        background = a.getResourceId(R.styleable.HorizontalEditTextItem_edit_item_background, 0);
        a.recycle();
        init();
    }

    private void init() {
        mView = View.inflate(mContext, R.layout.item_horizontal_edit_text, null);
        root_ll = (LinearLayout) mView.findViewById(R.id.root_Ll);
        mLeftImg = (ImageView) mView.findViewById(R.id.left_Img);
        mLeftText = (TextView) mView.findViewById(R.id.left_Text);
        mEditText = (EditText) mView.findViewById(R.id.editText);
        mEditText.setText(edit_text);
        mEditText.setTextSize(px2dip(edit_text_size));
        mEditText.setHint(edit_hint_text);
        mEditText.setTextColor(edit_text_color);
        mLeftText.setText(left_text);
        mLeftText.setTextColor(left_text_color);
        mLeftText.setTextSize(px2dip(left_text_size));
        mLeftText.setVisibility(left_text_visible);
        mLeftImg.setImageResource(left_img);
        mLeftImg.setVisibility(left_img_visible);
        LayoutParams layoutParams = new LayoutParams(left_img_size, left_img_size);
        mLeftImg.setLayoutParams(layoutParams);
        root_ll.setBackgroundResource(background);
        LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        addView(mView, lp);
    }

    private int dip2px(int dip) {
        return (int) (mDensity * dip + 0.5f);
    }

    /**
     * 设置hint
     *
     * @param str
     */
    public void setItemHint(String str) {
        mEditText.setHint(str);
    }

    /**
     * 条目的文本内容
     *
     * @param str
     */
    public void setItemText(String str) {
        mEditText.setText(str);
    }

    /**
     * 条目的文本的颜色
     *
     * @param color
     */
    public void setItemTextColor(int color) {
        mEditText.setTextColor(color);
    }

    /**
     * 条目文本的大小
     *
     * @param size
     */
    public void setItemTextSize(int size) {
        mEditText.setTextSize(size);
    }


    /**
     * 条目左边图片资源
     *
     * @param ids
     */
    public void setLeftImg(Integer ids) {
        mLeftImg.setImageResource(ids);
    }

    /**
     * 条目左边图片是否可见
     *
     * @param visibility
     */
    public void setLeftImgVisible(int visibility) {
        mLeftImg.setVisibility(visibility);
    }

    /**
     * 条目左边图片的大小
     *
     * @param size
     */
    public void setLeftImgSize(int size) {
        LayoutParams layoutParams = new LayoutParams(dip2px(size), dip2px(size));
        mLeftImg.setLayoutParams(layoutParams);
    }

    /**
     * 左边的文本内容
     *
     * @param str
     */
    public void setLeftText(String str) {
        mLeftText.setText(str);
    }

    /**
     * 左边的文本的颜色
     *
     * @param color
     */
    public void setLeftTextColor(int color) {
        mLeftText.setTextColor(color);
    }

    /**
     * 左边文本的大小
     *
     * @param size
     */
    public void setLeftTextSize(int size) {
        mLeftText.setTextSize(size);
    }

    /**
     * 设置左边文字是否隐藏
     *
     * @param visibility
     */
    public void setLeftTextVisible(int visibility) {

        mLeftText.setVisibility(visibility);
    }


    public int px2dip( float pxValue) {
        return (int) (pxValue / mDensity + 0.5f);
    }
}
